package com.jojo.bubble.controller;

import com.alipay.api.AlipayApiException;
import com.jojo.bubble.entity.Agent;
import com.jojo.bubble.entity.Balance;
import com.jojo.bubble.service.AgentService;
import com.jojo.bubble.service.BalanceService;
import com.jojo.bubble.common.domain.AjaxResult;
import com.jojo.bubble.utils.AlipayFundTransUniTransfer;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.util.ObjectUtils;
import com.jojo.bubble.common.enums.JsonResultEnum;

import javax.annotation.Resource;
import java.util.List;

/**
 * 提现表(Balance)表控制层
 *
 * @author jojo
 * @since 2023-08-21 04:46:42
 */
@RestController
@RequestMapping("/admin/balance")
public class BalanceController {
    /**
     * 服务对象
     */
    @Resource
    private BalanceService balanceService;
    @Resource
    private AgentService agentService;

    @GetMapping("/list")
    public AjaxResult getList(Balance record) {
        List<Balance> balanceList = balanceService.getList(record);
        for (Balance balance : balanceList) {
            Agent agent = agentService.getInfo(balance.getAgentId());
            if (!ObjectUtils.isEmpty(agent)) {
                balance.setAgentName(agent.getName());
            }
        }
        return AjaxResult.success(balanceList);
    }

    @GetMapping("/info")
    public AjaxResult getInfo(Integer id) {
        Balance balance = balanceService.getInfo(id);
        return AjaxResult.success(balance);
    }

    @PostMapping("/insert")
    public AjaxResult insert(Balance record) {
        balanceService.insert(record);
        return AjaxResult.success();
    }

    @PostMapping("/update")
    public AjaxResult update(Balance record) {
        Balance balance = balanceService.getInfo(record.getId());
        if (ObjectUtils.isEmpty(balance)) {
            return AjaxResult.error(JsonResultEnum.RECORD_NULL);
        }
        balanceService.update(record);
        return AjaxResult.success();
    }

    @PostMapping("/fund")
    public AjaxResult aliFund(Balance record) throws AlipayApiException {
        Balance balance = balanceService.getInfo(record.getId());
        if (ObjectUtils.isEmpty(balance)) {
            return AjaxResult.error(JsonResultEnum.RECORD_NULL);
        }
        String orderNo = String.valueOf(System.currentTimeMillis());
        String price = String.valueOf(balance.getMoney());
        String code = AlipayFundTransUniTransfer.aliFund(orderNo, balance.getName(), balance.getAccount(), price);
        if (code.equals("10000")) {
            balance.setStatus(1);
            balanceService.update(balance);
//            System.out.println("code:" + code);
            return AjaxResult.success();
        } else {
            return AjaxResult.error(code);
        }

    }

    @PostMapping("/cancel")
    public AjaxResult cancel(Balance record) {
        Balance balance = balanceService.getInfo(record.getId());
        if (ObjectUtils.isEmpty(balance)) {
            return AjaxResult.error(JsonResultEnum.RECORD_NULL);
        }
        balance.setStatus(2);
        balanceService.update(record);
        Agent agent = agentService.getInfo(balance.getAgentId());
        agent.setBalance(agent.getBalance().add(balance.getMoney()));
        agentService.update(agent);
        return AjaxResult.success();
    }

    @PostMapping("/delete")
    public AjaxResult delete(int id) {
        balanceService.delete(id);
        return AjaxResult.success();
    }
}

